﻿using System;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Text.RegularExpressions;

namespace MT.CustomWindow
{
    /// <summary>
    /// FeedbackWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FeedbackWindow : Window
    {
        public static Action<bool> WindowShowAction;

        public FeedbackWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Btn_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Commit_Click(object sender, RoutedEventArgs e)
        {
            if (!MessageBoxWindow.ShowMessage("请确认是否提交？", MessageBoxType.With_OkCancel))
                return;

            if (string.IsNullOrEmpty(TextBox_Name.Text))
            {
                MessageBoxWindow.ShowMessage("请输入正确的姓名！", MessageBoxType.With_OkCancel);
                return;
            }

            Regex rePhone = new Regex(@"^[1]+[3,4,5,7,8]+\d{9}");
            bool re = rePhone.IsMatch(TextBox_Phone.Text);
            if (!re)
            {
                MessageBoxWindow.ShowMessage("请输入正确的手机号！", MessageBoxType.With_OkCancel);
                return;                  
            }

            Regex reEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
            bool re1 = reEmail.IsMatch(TextBox_Email.Text);
            if (!re1)
            {
                MessageBoxWindow.ShowMessage("请输入正确的邮箱！", MessageBoxType.With_OkCancel);
                return;
            }

            string baseMsg = $"{TextBox_Name.Text};{TextBox_Phone.Text};{TextBox_Email.Text}";
            string questionType = string.Empty;
            foreach (var item in UniformGrid_QuestionType.Children)
            {
                CheckBox checkBox = (CheckBox)item;
                if ((bool)checkBox.IsChecked)
                {
                    questionType += $"{checkBox.Content};";
                }                
            }
            questionType = questionType.TrimEnd(';');
            if (string.IsNullOrEmpty(questionType))
            {
                MessageBoxWindow.ShowMessage("请选择正确的问题类型！", MessageBoxType.With_OkCancel);
                return;
            }

            if (string.IsNullOrEmpty(TextBox_QuestionDes.Text))
            {
                MessageBoxWindow.ShowMessage("填写问题描述！", MessageBoxType.With_OkCancel);
                return;
            }

            string emailStr = $"{baseMsg}\r\n{questionType}\r\n{TextBox_QuestionDes.Text}";

            //SendEmailSina(emailStr);
            SendEmailQQ(emailStr);

            MessageBoxWindow.ShowMessage("提交完成！", MessageBoxType.With_OkCancel);
            this.Close();
        }

        #region 意见反馈
        /// <summary>
        /// 通过新浪邮箱发送客户意见到公司服务邮箱 
        /// </summary>
        /// <param name="str"></param>
        public static void SendEmailSina(string str)
        {
            string emailAddr = "elysium1984@sina.com";
            string emailAuthorizationCode = "19840711zl";

            MailMessage myMail = new MailMessage();
            myMail.From = new MailAddress(emailAddr);
            myMail.To.Add(new MailAddress("service@huiyigu.cn"));

            //myMail.To.Add(new MailAddress("982447870@qq.com"));
            myMail.Subject = "望诊教学2.0意见反馈";
            myMail.SubjectEncoding = Encoding.UTF8;
            myMail.Body = str;
            myMail.BodyEncoding = Encoding.UTF8;
            myMail.IsBodyHtml = true;

            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.sina.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = new NetworkCredential(emailAddr, emailAuthorizationCode);
                smtp.Send(myMail);
            }
            catch (SmtpException ex)
            {
                MessageBoxWindow.ShowMessage(ex.Message);
            }
        }

        /// <summary>
        /// 通过qq邮箱发送客户意见到公司服务邮箱的方法
        /// </summary>
        /// <param name="str"></param>
        public static void SendEmailQQ(string str)
        {
            string emailAddr = "276367605@qq.com";
            string emailAuthorizationCode = "hhcfffrceimhcajh";

            //string emailAddr = "645356939@qq.com";
            //string emailAuthorizationCode = "ipuqjenhggoubceb";

            MailMessage myMail = new MailMessage();
            myMail.From = new MailAddress(emailAddr);
            myMail.To.Add(new MailAddress("service@huiyigu.cn"));
            //myMail.To.Add(new MailAddress("734346746@qq.com"));
            myMail.Subject = "望诊教学2.0意见反馈";
            myMail.SubjectEncoding = Encoding.UTF8;
            myMail.Body = str;
            myMail.BodyEncoding = Encoding.UTF8;
            myMail.IsBodyHtml = true;

            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.qq.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = true;
                smtp.EnableSsl = true;
                smtp.Credentials = new NetworkCredential(emailAddr, emailAuthorizationCode);
                smtp.Send(myMail);
            }
            catch (SmtpException ex)
            {
                MessageBoxWindow.ShowMessage(ex.Message);
            }
        }

        #endregion

        private void TextBox_Phone_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9]+");
            e.Handled = re.IsMatch(e.Text);
        }

        private void TextBox_Email_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string patten1 = @"^\w+@\w+.[a-z]{3-6}$";
            Regex re = new Regex(@"^[1]+[3,4,5,7,8]+\d{9}");
            e.Handled = Regex.IsMatch(e.Text, patten1);
        }
    }
}
